
# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

require 'net/https'
require 'net/http'
require 'net/http/post/multipart'
require 'json'

default_platform(:ios)
xcode_select(ENV["XCODE_SELECT"])

platform :ios do
  #before_all do
    #setup_circle_ci
  #end

  desc "Build and push a new beta build to TestFlight"
  desc "Samples: fastlane ios beta configuration:Release skip_waiting:true"
  lane :beta do |options|
    #ensure_git_status_clean
    configuration = options[:configuration]
    skip_waiting = options[:skip_waiting]
    if skip_waiting.nil?
      skip_waiting = true
    end

    pod_install
    bump_build_number
    build(configuration:configuration)

    upload_testflight(skip_waiting:skip_waiting)

    if !skip_waiting 
      upload_beta_dSYM_to_bugly
    end
  end


  desc "Build and push a new beta_staging build to TestFlight"
  desc "Samples: fastlane beta_release skip_waiting:true"
  lane :beta_release do |options|
    #ensure_git_status_clean
    skip_waiting = options[:skip_waiting]
    if skip_waiting.nil?
      skip_waiting = true
    end

    beta(configuration:"Release", skip_waiting:skip_waiting)
  end

  desc "Build and push a new beta_staging build to TestFlight"
  desc "Samples: fastlane beta_preproduction skip_waiting:true"
  lane :beta_preproduction do |options|
    #ensure_git_status_clean
    skip_waiting = options[:skip_waiting]
    if skip_waiting.nil?
      skip_waiting = true
    end

    beta(configuration:"Preproduction", skip_waiting:skip_waiting)
  end

  desc "Build and push a new beta_staging build to TestFlight"
  desc "Samples: fastlane beta_staging skip_waiting:true"
  lane :beta_staging do |options|
    #ensure_git_status_clean
    skip_waiting = options[:skip_waiting]
    if skip_waiting.nil?
      skip_waiting = true
    end

    beta(configuration:"Staging", skip_waiting:skip_waiting)
  end
  
  desc "Build and push a new beta build to Appcenter"
  lane :appcenter_lane do
    pod_install
    bump_build_number
    adhoc
    upload_appcenter
  end

  desc "Build and push a new beta build to fir.im"
  lane :fir_lane do
    app_name = ENV['APP_NAME']
    pod_install
    bump_build_number
    adhoc
    upload_fir
    upload_adhoc_dSYM_to_bugly
  end

  desc "Build and push a new beta build to fir.im"
  desc "Samples: fastlane ios fir_inhouse_lane configuration:Staging/Release/Debug/Preproduction"
  lane :fir_inhouse_lane do |options|
    configuration = options[:configuration]

    app_name = ENV['APP_NAME']
    pod_install
    bump_build_number
    inhouse(configuration: configuration)
    upload_fir
    upload_adhoc_dSYM_to_bugly
  end

  desc "Build and push a new staging build to Pgyer"
  lane :pgyer_lane do
    app_name = ENV['APP_NAME']
    pod_install
    bump_build_number
    adhoc
    upload_pgyer
    upload_adhoc_dSYM_to_bugly
  end

  desc "Build and push a new staging build to Pgyer"
  desc "Samples: fastlane ios pgyer_inhouse_lane configuration:Staging/Release/Debug/Preproduction"
  lane :pgyer_inhouse_lane do |options|
    configuration = options[:configuration]

    app_name = ENV['APP_NAME']
    pod_install
    bump_build_number
    inhouse(configuration: configuration)
    upload_pgyer
    upload_adhoc_dSYM_to_bugly
  end

  desc "Upload beta dSYM.zip to Bugrly"
  desc "Samples: fastlane upload_beta_dSYM_to_bugly build_number:xxxxxxxxxx"
  lane :upload_beta_dSYM_to_bugly do |options|
    #ensure_git_status_clean
    build_number = options[:build_number]
    if build_number.nil?
      build_number = get_build_number
    end

    bundle_id = ENV['BUNDLE_IDENTIFIER']
    app_version = get_version_number(
      target: ENV['SCHEME']
    )
    filepath = "dsym/#{bundle_id}-#{app_version}-#{build_number}.dSYM.zip"
    puts filepath
    download_itc_dsyms(build_number:build_number)
    upload_bugly_dSYM(filepath:filepath)
  end

  desc "Upload adhoc dSYM.zip to Bugrly"
  desc "Samples: fastlane upload_adhoc_dSYM_to_bugly"
  lane :upload_adhoc_dSYM_to_bugly do
    output_directory = ENV['ADHOC_OUTPUT_DIRECTORY']

    filepath = "#{output_directory}/app.app.dSYM.zip"
    puts filepath
    upload_bugly_dSYM(filepath:filepath)
  end

  desc "Upload dSYM.zip to Bugrly"
  desc "Samples: fastlane upload_bugly_dSYM filepath:build-app-store/app.app.dSYM.zip"
  lane :upload_bugly_dSYM do |options|
    UI.message "upload dSYM.zip to bugly ..."
    filepath = options[:filepath]
    bugly_app_id = ENV['BUGLY_APP_ID']
    bugly_app_key = ENV['BUGLY_APP_KEY']
    bundle_id = ENV['BUNDLE_IDENTIFIER']
    app_version = get_version_number(
      target: ENV['SCHEME']
    )
    build_number = get_build_number
    product_version = "#{build_number}@#{app_version}"
    upload_bugly(bugly_app_id, bugly_app_key, bundle_id, filepath, product_version)
  end


  desc "Test"
  lane :test do
    scheme = ENV['SCHEME']
    run_tests(
      scheme: scheme
  )
  end

  desc "Lane: Do Test"
  lane :lane_test do
    pod_install
    test
  end

end



# Tools for upload to bugly
def upload_bugly(bugly_app_id, bugly_app_key, bundle_id, filepath, product_version)

  json_file = 'upload_app_to_bugly_result.json'
  cmd = "curl -k \"https://api.bugly.qq.com/openapi/file/upload/symbol?app_key=#{bugly_app_key}&app_id=#{bugly_app_id}\" --form \"api_version=1\" --form \"app_id=#{bugly_app_id}\" --form \"app_key=#{bugly_app_key}\" --form \"symbolType=2\" --form \"bundleId=#{bundle_id}\" --form \"productVersion=#{product_version}\" --form \"channel=fastlane\" --form \"fileName=#{filepath}\" --form \"file=@#{filepath}\" -o " + json_file + " --verbose"
  puts cmd
  sh(cmd)
  res = JSON.parse(File.read(json_file))

  UI.message "#{res}"

  ret = res['rtcode']
  msg = res['msg']

  if ret == 0
    UI.message "upload success"
  else
    UI.message "upload failed,result is #{msg}"
  end

end


